#include "recbox.h"
#include "ui_recbox.h"
#include "recboxitem.h"

RecBox::RecBox(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::RecBox)
{
    ui->setupUi(this);

    //设置左右按钮图片
    QIcon up_icon(":/images/upPage.png");
    ui->btUp->setIcon(up_icon);
    QIcon down_icon(":/images/downPage.png");
    ui->btDown->setIcon(down_icon);
}



RecBox::~RecBox()
{
    delete ui;
}

void RecBox::setRecBoxItem(QVector<QPair<QString, QString> > &pic, int row, int col)
{
    pic_ = pic;
    row_ = row;
    col_ = col;
    idx_ = 0;
    addRecBoxItem();
}

void RecBox::addRecBoxItem()
{
    //删除recBox内部之前的item
    QList<RecBoxItem*> listUp = ui->recListUp->findChildren<RecBoxItem*>();
    for(auto& item : listUp){
        ui->recListUpHLayout->removeWidget(item);
        delete item;
    }
    QList<RecBoxItem*> listDown = ui->recListDown->findChildren<RecBoxItem*>();
    for(auto& item : listDown){
        ui->recListDownHLayout->removeWidget(item);
        delete item;
    }

    //添加新元素
    for(int i=idx_; i<(idx_+row_*col_); ++i){
        RecBoxItem *item  = new RecBoxItem();
        item->setRecImage(":/images/rec/"+pic_[i].first);
        item->setRecText(pic_[i].second);
        if(row_==2 && i>=(idx_+col_)){
            ui->recListDownHLayout->addWidget(item);
        }else{
            ui->recListUpHLayout->addWidget(item);
        }
    }
}

void RecBox::on_btUp_clicked()
{
    if(idx_-row_*col_ < 0) return;
    idx_ -= row_*col_;
    addRecBoxItem();
}


void RecBox::on_btDown_clicked()
{
    if(idx_+2*row_*col_-1 >= pic_.size()) return;
    idx_ += row_*col_;
    addRecBoxItem();
}

